package com.imooc.service.impl;

import com.imooc.bo.UserInfoBo;
import com.imooc.enums.Sex;
import com.imooc.enums.UserInfoModifyType;
import com.imooc.enums.YesOrNo;
import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.service.UsersService;
import com.imooc.utils.DateUtil;
import com.imooc.utils.DesensitizationUtil;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UsersServiceImpl implements UsersService {

    private static final String USER_FACE1 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxF6ZUySASMbOAABBAXhjY0Y649.png";
    @Resource
   private   UsersMapper usersMapper;
    @Autowired
    private Sid sid;

    @Override
    public Users queryMobileIsExist(String mobile) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobile",mobile);
        Users users = usersMapper.selectOneByExample(example);
        return users;
    }

    @Transactional
    @Override
    public Users createUsers(String mobile) {
        // 获得全局唯一主键
        String UserId = sid.nextShort();
        Users user = new Users();
        user.setId(UserId);
        user.setMobile(mobile);
        user.setNickname("用户：" + DesensitizationUtil.commonDisplay(mobile));
        user.setImoocNum("用户：" + DesensitizationUtil.commonDisplay(mobile));
        user.setFace(USER_FACE1);

        user.setBirthday(DateUtil.stringToDate("1900-01-01"));
        user.setSex(Sex.secret.type);

        user.setCountry("中国");
        user.setProvince("");
        user.setCity("");
        user.setDistrict("");
        user.setDescription("这家伙很懒，什么都没留下~");
        user.setCanImoocNumBeUpdated(YesOrNo.YES.type);

        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());

        usersMapper.insert(user);
        return user;
    }

    @Override
    public Users getUser(String userId) {
        return usersMapper.selectByPrimaryKey(userId);
    }

    @Transactional
    @Override
    public Users updatedUserInfo(UserInfoBo userInfoBo) {
        Users user = new Users();
        BeanUtils.copyProperties(userInfoBo,user);
        int result = usersMapper.updateByPrimaryKeySelective(user);
        if (result != 1){
            GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        }
        return getUser(userInfoBo.getId());
    }

    @Transactional
    @Override
    public Users updatedUserInfo(UserInfoBo userInfoBo, Integer type) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();

        if (UserInfoModifyType.NICKNAME.type == type){
            criteria.andEqualTo("nickname",userInfoBo.getNickname());
            Users user = usersMapper.selectOneByExample(example);
            if (user != null){
                GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_NICKNAME_EXIST_ERROR);
            }

        }
        if (UserInfoModifyType.IMOOCNUM.type == type){
            criteria.andEqualTo("imoocNum",userInfoBo.getImoocNum());
            Users user = usersMapper.selectOneByExample(example);
            //如果慕课号存在一样的
            if (user != null){
                GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_IMOOCNUM_EXIST_ERROR);
            }
            //如果修改了慕课名 由于慕课名只能修改一次 我们需要将CanImoocNumBeUpdated 设置成0
            Users TempUser = getUser(userInfoBo.getId());
            //如果已经是0
            if (TempUser.getCanImoocNumBeUpdated() == YesOrNo.NO.type){
                GraceException.display(ResponseStatusEnum.USER_INFO_CANT_UPDATED_IMOOCNUM_ERROR);
            }
            userInfoBo.setCanImoocNumBeUpdated(0);
        }

            return  updatedUserInfo(userInfoBo);
    }
}
